package com.app.business.web.business;

import com.app.business.model.ZsProject;
import com.app.business.model.ZsProjectUser;
import com.app.business.service.ZsProjectService;
import com.app.business.service.ZsProjectUserService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.SysRole;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.permission.service.SysRoleService;
import com.app.system.permission.service.UserInfoService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户分配项目管理
 * Created by lsh on 2019/2/15.
 * @author lsh
 */
@Controller
@RequestMapping("/business/zsProjectUser/*")
public class ZsProjectUserController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ZsProjectUserService zsProjectUserService;

    /**
     * 所有用户列表
     */
    @RequestMapping("userList")
    @RequiresPermissions("zsProjectUser:userList")
    public void userList(ModelMap modelMap) {
        List<SysRole> sysRoleList = this.sysRoleService.findAll();
        modelMap.addAttribute("sysRoleList", sysRoleList);
    }


    @RequestMapping("loadUserList")
    @ResponseBody
    @RequiresPermissions("zsProjectUser:loadUserList")
    public Map loadUserList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<UserInfo> housePageBean = this.userInfoService.findPageBean();

        Map<String, DataDict> stateMap = this.dataDictService.getMapByParentNameForValue("用户状态");

        List<UserInfo> userInfoList = housePageBean.getDatas();
        if (!CollectionUtils.isEmpty(userInfoList)) {
            Map<String, Object> item = null;
            for (UserInfo userInfo : userInfoList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", userInfo.getId());
                item.put("name", userInfo.getName());
                item.put("username", userInfo.getUsername());//账号
                item.put("telPhone", userInfo.getTelPhone());
                item.put("description", userInfo.getDescription());
                item.put("departmentName", userInfo.getDepartment() == null ? "" : userInfo.getDepartment().getName());

                item.put("roles", getRoleNames(userInfo.getRoleList()));
                item.put("state", StringUtils.dataDictShow(stateMap.get(String.valueOf(userInfo.getState()))));
                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }


    @RequestMapping("form")
    @SuppressWarnings("zsProjectUser:form")
    public void form(HttpServletRequest request, ModelMap modelMap) {
        int userInfoId = ParamUtils.getInt(request, "userInfoId", 0);
        UserInfo userInfo = this.userInfoService.findOne(userInfoId);
        Precondition.checkSqdsArguement(userInfo != null, "用户信息不存在！");
        List<ZsProject> zsProjectList = this.zsProjectService.zsProjectAllList();
        List<Integer> zsProjectIdList = this.zsProjectUserService.getZsProjectIdByUserInfoId(userInfo.getId());


        modelMap.addAttribute("userInfo", userInfo);
        modelMap.addAttribute("roles", getRoleNames(userInfo.getRoleList()));
        modelMap.addAttribute("zsProjectList", zsProjectList);
        modelMap.addAttribute("zsProjectIdList", zsProjectIdList);
    }


    /**
     * 用户分配征收项目保存
     * @param request
     * @return
     */
    @RequestMapping("zsProjectUserSave")
    @ResponseBody
    @RequiresPermissions("zsProjectUser:zsProjectUserSave")
    public Response zsProjectUserSave(HttpServletRequest request) {
        this.zsProjectUserService.formSave(request);
        return new Response("0001", "操作成功");
    }


    private String getRoleNames(Set<SysRole> set) {
        StringBuffer sb = new StringBuffer("");
        if (CollectionUtils.isNotEmpty(set)) {
            for (SysRole sysRole : set) {
                if (sb.length() > 0) {
                    sb.append("、");
                }
                sb.append(sysRole.getRole());
            }
        }
        return sb.toString();
    }
}
